package com.nondo.rdp.auth.domain;

import com.fasterxml.jackson.annotation.*;
import com.nondo.rdp.auth.common.DateUtils;
import com.nondo.rdp.auth.security.auth.ajax.RegisterPhoneRequest;
import com.nondo.rdp.auth.security.auth.ajax.RegisterRequest;
import com.nondo.rdp.core.common.CommUtil;
import com.nondo.rdp.core.domain.base.BaseEntity;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import javax.persistence.*;
import java.util.Date;

/**
 * 用户Entity
 *
 * @author slacrey
 * @version 2014-6-17
 */
@Entity
@Table(name = "sys_user")
//@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class User extends BaseEntity {

    private static final long serialVersionUID = 1L;
    private Long id;        // 编号
    private String username;// 登录名
    private String nickName;// 昵称
    private String password;// 密码
    private String payPassword;// 交易密码
    private String no;        // 工号
    private String name;    // 姓名
    private String email;    // 邮箱
    private String phone;    // 电话
    private String mobile;    // 手机
    private String avatar;    // 用户头像地址
    private String userType;// 用户类型
    private String loginIp;    // 最后登陆IP
    private Date loginDate;    // 最后登陆日期
    private Integer loginCount;    // 登录次数
    private Integer sex;    // 性别
    private String cardNum;    // 身份证
    private String permanentAddress;    // 原籍
    private String emergencyContact;    // 紧急联系人
    private String emergencyContactNum;    // 紧急联系人电话
    private String emergencyAddress;    // 紧急联系人地址
    private Integer login = 0;    // 是否需要登录
    private Integer age;    // 年龄
    private Date birth;    // 生日年月

    private Role role;

    private long points; //积分
    private long inviterId; //邀请人ID
    private long vip; //是否为vip，0=不是，1=是
    private Date expireTime; //vip过期时间
    private String sign; //签名

    private String deviceId; //推送设备ID

    @JsonCreator
    public User() {
        super();
    }

    public User(RegisterRequest register) {
        this();
        this.username = register.getUsername();
        this.password = register.getPassword();
    }

    public User(RegisterPhoneRequest register) {
        this();
        this.username = register.getUsername();
        this.password = register.getPassword();
        this.phone = register.getPhone();
    }

    public User(Long id) {
        this();
        this.id = id;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
//    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_sys_user")
//    @SequenceGenerator(name = "seq_sys_user", sequenceName = "seq_sys_user")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(length = 22)
    public String getUsername() {
        return username;
    }

    @JsonSetter
    public void setUsername(String username) {
        this.username = username;
    }


    @JsonIgnore
    public String getPassword() {
        return password;
    }

    @JsonSetter
    public void setPassword(String password) {
        this.password = password;
    }

    @JsonIgnore
    public String getName() {
        return name;
    }

    @JsonIgnore
    public String getNo() {
        return no;
    }

    @JsonSetter
    public void setNo(String no) {
        this.no = no;
    }

    @JsonSetter
    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    @JsonSetter
    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    @JsonSetter
    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getMobile() {
        return mobile;
    }

    @JsonSetter
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getAvatar() {
        return CommUtil.getUserAvatar(avatar);
    }

    @JsonSetter
    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    @Transient
    @JsonIgnore
    public String getRemarks() {
        return remarks;
    }

    public String getUserType() {
        return userType;
    }

    @JsonSetter
    public void setUserType(String userType) {
        this.userType = userType;
    }

    @Transient
    @JsonIgnore
    public Date getCreateDate() {
        return createDate;
    }

    public String getLoginIp() {
        return loginIp;
    }

    @JsonSetter
    public void setLoginIp(String loginIp) {
        this.loginIp = loginIp;
    }

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date getLoginDate() {
        return loginDate;
    }

    @JsonSetter
    public void setLoginDate(Date loginDate) {
        this.loginDate = loginDate;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(name = "role_id")
    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public String getCardNum() {
        return cardNum;
    }

    public void setCardNum(String cardNum) {
        this.cardNum = cardNum;
    }

    public String getPermanentAddress() {
        return permanentAddress;
    }

    public void setPermanentAddress(String permanentAddress) {
        this.permanentAddress = permanentAddress;
    }

    public String getEmergencyContact() {
        return emergencyContact;
    }

    public void setEmergencyContact(String emergencyContact) {
        this.emergencyContact = emergencyContact;
    }

    public String getEmergencyContactNum() {
        return emergencyContactNum;
    }

    public void setEmergencyContactNum(String emergencyContactNum) {
        this.emergencyContactNum = emergencyContactNum;
    }

    public String getEmergencyAddress() {
        return emergencyAddress;
    }

    public void setEmergencyAddress(String emergencyAddress) {
        this.emergencyAddress = emergencyAddress;
    }

    public Integer getLogin() {
        return login;
    }

    public void setLogin(Integer login) {
        this.login = login;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    @Transient
    @JsonIgnore
    public Integer getAge() {
        age = DateUtils.getCurrentAgeByBirth(birth);
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Column(name = "nick_name")
    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    @Column(name = "pay_password")
    public String getPayPassword() {
        return payPassword;
    }

    public void setPayPassword(String payPassword) {
        this.payPassword = payPassword;
    }

    @Column(name = "login_count")
    public Integer getLoginCount() {
        return loginCount;
    }

    public void setLoginCount(Integer loginCount) {
        this.loginCount = loginCount;
    }

    @Column(name = "points", columnDefinition = "int default 0")
    public Long getPoints() {
        return points;
    }

    public void setPoints(Long points) {
        this.points = points;
    }

    @Column(name = "inviter_id", columnDefinition = "int default 0")
    public Long getInviterId() {
        return inviterId;
    }

    public void setInviterId(Long inviterId) {
        this.inviterId = inviterId;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    @Column(name = "vip", columnDefinition = "int default 0")
    public long getVip() {
        return vip;
    }

    public void setVip(long vip) {
        this.vip = vip;
    }

    public Date getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(Date expireTime) {
        this.expireTime = expireTime;
    }

    @Column(name = "sign")
    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }
}